#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>

#define SEP " "
#define SEP_LEN strlen(SEP) // 不要使用sizeof
#define LINE_SEP "\r\n"
#define LINE_SEP_LEN strlen(LINE_SEP) // 不要使用sizeof

enum
{
    OK = 0,
    DIV_ZERO,
    MOD_ZERO,
    OP_ERROR
};

std::string enLength(const std::string &text)
{
    std::string send_string = std::to_string(text.size());
    send_string += LINE_SEP;
    send_string += text;
    send_string += LINE_SEP;

    return send_string;
}

bool deLength(const std::string &package, std::string *text)
{
    auto pos = package.find(LINE_SEP);
    if (pos == std::string::npos)
        return false;
    std::string text_len_string = package.substr(0, pos);
    int text_len = std::stoi(text_len_string);
    *text = package.substr(pos + LINE_SEP_LEN, text_len);
    return true;
}

// 请求和响应都需要序列化和反序列化
class Request
{
public:
    Request()
    {
    }

    Request(int x_, int y_, char op_)
        : x(x_), y(y_), op(op_)
    {
    }

    bool serialize(std::string *out)
    {
        *out = "";
        std::string x_string = std::to_string(x);
        std::string y_string = std::to_string(y);
        *out = x_string;
        *out += SEP;
        *out += op;
        *out += SEP;
        *out += y_string;
        *out += LINE_SEP;
    }

    bool deserialize(const std::string &in)
    {
        auto left = in.find(SEP);
        auto right = in.find(SEP);
        if (left == std::string::npos || right == std::string::npos)
            return false;
        if (left == right)
            return false;
        if (right - (left + SEP_LEN))
            return false;

        std::string x_string = in.substr(0, left);
        if (x_string.empty())
            return false;
        std::string y_string = in.substr(right + SEP_LEN);
        if (y_string.empty())
            return false;
        x = std::stoi(x_string);
        y = std::stoi(y_string);

        op = in[left + SEP_LEN];
        return true;
    }

public:
    int x;
    int y;
    char op;
};

class Response
{
public:
    Response()
    {
    }

    Response(int exitcode_, int result_)
        : exitcode(exitcode_), result(result_)
    {
    }

    bool serialize(std::string *out)
    {
        *out = "";
        std::string ec_string = std::to_string(exitcode);
        std::string res_string = std::to_string(result);

        *out = ec_string;
        *out += SEP;
        *out += res_string;
        *out += LINE_SEP;
        return true;
    }

    bool deserialize(const std::string &in)
    {
        auto mid = in.find(SEP);
        if (mid == std::string::npos)
            return false;
        std::string ec_string = in.substr(0, mid);
        std::string res_string = in.substr(mid + SEP_LEN);

        if (ec_string.empty() || res_string.empty())
            return false;

        exitcode = std::stoi(ec_string);
        result = std::stoi(res_string);
        return true;
    }

public:
    int exitcode;
    int result;
};

bool recvPackage(int sock, std::string &inbuffer,std::string *text)
{
    char buffer[1024];
    while (true)
    {
        ssize_t n = recv(sock, buffer, sizeof(buffer) - 1, 0);
        if (n > 0)
        {
            buffer[n] = 0;
            inbuffer += buffer;
            auto pos = inbuffer.find(LINE_SEP);
            if (pos == std::string::npos)
                continue;
            std::string text_len_string = inbuffer.substr(0, pos);
            int text_len = std::stoi(text_len_string);
            int total_len = text_len_string.size() + 2 * LINE_SEP_LEN + text_len;
            if (inbuffer.size() < total_len)
                continue;

            *text = inbuffer.substr(0, total_len);
            inbuffer.erase(0, total_len);
            break;
        }
        else
            return false;
    }
    return true;
}